package di.ass.view;

import java.util.ArrayList;

import di.ass.database.DatabaseHandler;
import di.ass.database.Food;
import di.ass.edit.AlertsEdit;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;
import di.ass.R;

public class ListFoodAdapter extends ArrayAdapter<Food> {

	Context context;
	int resource;
	ArrayList<Food> arrayFood;
	DatabaseHandler db;
	TextView nameFood;
	TextView quantity;

	public ListFoodAdapter(Context context, int textViewResourceId,
			ArrayList<Food> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		resource = textViewResourceId;
		arrayFood = objects;
		db = new DatabaseHandler(context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View vi = convertView;

		if (vi == null) {
			vi = new ElementListFood(context);
		}

		final Food data = arrayFood.get(position);

		if (data != null) {
			nameFood = ((ElementListFood) vi).getNameFood();
			quantity = ((ElementListFood) vi).getQuantity();
			final ToggleButton toggleButton = ((ElementListFood) vi)
					.getToggleButton();
			final CheckBox isCheckedFood = ((ElementListFood) vi)
					.getCheckFood();
			final LinearLayout linearLayout = ((ElementListFood) vi)
					.getLinearLayout();
			String[] textTogg = null;
			if (db.getLanguageUse().equals("Việt Nam")) {
				;
				textTogg = context.getResources().getStringArray(
						R.array.textview_vi);
			} else if (db.getLanguageUse().equals("English")) {
				textTogg = context.getResources().getStringArray(
						R.array.textview_eng);
			}
			toggleButton.setTextOn(textTogg[7]);
			toggleButton.setTextOff(textTogg[8]);
			// lay du lieu
			nameFood.setText(data.getName());
			quantity.setText(data.getQuantity() + " " + data.getUnitary());
			linearLayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					boolean isCheck = data.getChecked();
					isCheckedFood.setChecked(!isCheck);
					data.setChecked(!isCheck);
				}
			});
			linearLayout.setOnLongClickListener(new OnLongClickListener() {

				@Override
				public boolean onLongClick(View arg0) {
					// TODO Auto-generated method stub
					AlertDialog alertDialog = AlertsEdit.ShowEditFoodDialog(
							context, db.getOneFood(data.getId()));
					alertDialog.setOnDismissListener(new OnDismissListener() {
						@Override
						public void onDismiss(DialogInterface dialog) {
							load(db.getOneFood(data.getId()));
							notifyDataSetChanged();
						}
					});
					alertDialog.show();
					return false;
				}
			});
			
			isCheckedFood
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton arg0,
								boolean arg1) {
							// TODO Auto-generated method stub
							data.setChecked(arg1);
						}
					});
			isCheckedFood.setChecked(data.getChecked());

			toggleButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (toggleButton.isChecked()) {
						data.setBought(true);
						linearLayout
								.setBackgroundResource(R.drawable.bg_element_list_food_bought);
						db.updateFood(data);
					} else {
						data.setBought(false);
						linearLayout
								.setBackgroundResource(R.drawable.bg_element_list_food_not_buy_yet);
						db.updateFood(data);
					}
				}
			});
			if (db.getBoughtFood(data) == 1) {
				toggleButton.setChecked(true);
				linearLayout
						.setBackgroundResource(R.drawable.bg_element_list_food_bought);

			} else {
				toggleButton.setChecked(false);
				linearLayout
						.setBackgroundResource(R.drawable.bg_element_list_food_not_buy_yet);

			}
			load(db.getOneFood(data.getId()));
		}
		notifyDataSetChanged();
		return vi;
	}

	public void load(Food food) {
		if (food != null) {
			nameFood.setText(food.getName());
			quantity.setText(food.getQuantity() + " " + food.getUnitary());
		}
	}

}